import logging

import pytest

from resource.pom.settings import Settings
from resource.pom.system_ui import SystemUI
from utils.manager import Direction, Images, MatchRules
from utils.device import Device


class Test:
    app = Settings
    system_ui = SystemUI

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        logging.info('步骤1.打开设置')
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        device.wait(1)
        logging.info('预期结果1.进入设置页面')
        device.assert_current_window(self.app.window_name)

        logging.info('步骤2.点击蓝牙')
        device.dirc_fling(Direction.Up)
        device.click(*self.app.bluetooth_item)
        logging.info('预期结果2.进入蓝牙页面')
        device.refresh_layout()
        device.assert_type_exist('Toggle')
        device.assert_text_exist('设备名称')

        logging.info('步骤3.打开已配对列表设置，点击设置图标')
        # 检查wifi开关状态
        toggle = device.get_element_by_type('Toggle')
        if toggle.get('checked') == 'false':
            device.click_element(toggle)
        bluetooth = self.app.bluetooth()
        device.wait(10)
        device.refresh_layout()
        set_btn = 642, 530
        if device.get_element_by_text('已配对的设备'):
            bt_name = device.get_element_by_text(bluetooth['name'])
            btx, bty = device.center_of_element(bt_name)
            kyx, kyy = device.center_of_element(device.get_element_by_text('可用设备'))
            assert bty < kyy, '目标蓝牙设备不在已配对的设备中'
            device.click(*set_btn)
            logging.info('预期结果3.进入配对页面')
            device.refresh_layout()
            device.assert_text_exist('取消配对')

            logging.info('步骤4.点击取消配对')
            device.click_element(device.get_element_by_text('取消配对'))
            logging.info('预期结果4.取消配对成功，已配对蓝牙在列表消失且刷新到可用设备列表界面')
            device.wait(3)
            device.refresh_layout()
            device.assert_text_exist('已配对的设备', expect=False)
            btx, bty = device.center_of_element(bt_name)
            kyx, kyy = device.center_of_element(device.get_element_by_text('可用设备'))
            assert bty > kyy, '已配对的蓝区未刷新到可用设备列表界面中'
        else:
            logging.info('没有已配对的设备，先配对，先将待连接的设备解锁')
            deviceB = Device(bluetooth['sn'])
            self.app.open_bluetooth_switch(deviceB)

            device.click_element(device.get_element_by_text(bluetooth['name']))
            device.wait(2)
            win_name, win_id = device.get_focus_window()
            device.refresh_layout()
            text_elements = device.get_elements_by_condition(condition={"hostWindowId": win_id})
            code = ''
            for e in text_elements:
                if e.get('text').isdigit():
                    code = e.get('text')
                    break
            assert code, '配对码为空'

            deviceB.refresh_layout()
            deviceB.assert_text_exist('蓝牙配对请求')
            deviceB.assert_text_exist('要与您配对。请确认此配对码', MatchRules.Contain)
            deviceB.assert_type_exist('Checkbox')
            deviceB.assert_text_exist('配对之后，向所配对设备授予通讯录和通话记录的访问权限。')
            deviceB.assert_text_exist(code)

            deviceB.click_element(deviceB.get_element_by_text('配对'))
            device.click_element(device.get_element_by_text('配对'))

            logging.info('检查配对是否成功')
            device.refresh_layout()
            device.assert_text_exist('已配对的设备')
            device.assert_text_exist(bluetooth['name'])

            logging.info('关掉蓝牙开关再打开开关，为了让连接状态快速刷新')
            device.click_element(toggle)
            device.click_element(toggle)
            device.wait(5)
            device.refresh_layout()
            btx, bty = device.center_of_element(device.get_element_by_text(bluetooth['name']))
            kyx, kyy = device.center_of_element(device.get_element_by_text('可用设备'))
            assert bty < kyy, '目标蓝牙设备不在已配对的设备中'
            device.click(*set_btn)
            logging.info('预期结果3.进入配对页面')
            device.refresh_layout()
            device.assert_text_exist('取消配对')

            logging.info('步骤4.点击取消配对')
            device.click_element(device.get_element_by_text('取消配对'))
            logging.info('预期结果4.取消配对成功，已配对蓝牙在列表消失且刷新到可用设备列表界面')
            device.wait(30)
            device.refresh_layout()
            device.assert_text_exist('已配对的设备', expect=False)
            btx, bty = device.center_of_element(device.get_element_by_text(bluetooth['name']))
            kyx, kyy = device.center_of_element(device.get_element_by_text('可用设备'))
            assert bty > kyy, '已配对的蓝区未刷新到可用设备列表界面中'
